package cn.ysnk.clz.controller.userManage;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;

import cn.ysnk.clz.config.annotation.ControllerAnnotation;
import cn.ysnk.clz.entity.dataUser.DataUser;
import cn.ysnk.clz.interceptor.security.role.RoleEnum;
import cn.ysnk.clz.util.MD5Util;

@ControllerAnnotation(url="/adminUser",roleType={RoleEnum.ROLE_ADMIN})
public class UserController extends Controller{

	public void index(){
		List<DataUser> dataUsers = DataUser.userDao.queryAllUser();
		setAttr("dataUsers", dataUsers);
		renderJsp("/admin/user/adminUser.jsp");
	}
	
	public void editUser(){
		String userId = getPara("userId");
		if(userId!=null&&!"".equals(userId))
			setAttr("userId", userId);
		renderJsp("/admin/user/editUser.jsp");
	}
	
	public void saveUser(){
		String userId = getPara("userId");
		String username = getPara("username");
		String password = getPara("password");
		String nickName = getPara("nickName");
		String roleType = getPara("roleType");
		String headIcon = getPara("headIcon");
		
		DataUser dUser = null;
		if(userId==null||"".equals(userId)){
			dUser = new DataUser();
			dUser.set("id", UUID.randomUUID().toString());
			dUser.set("isLock", false);
			dUser.set("registerDate", new Date());
		}else{
			dUser = DataUser.userDao.findById(userId);
		}
		dUser.set("username", username); 
		if("".equals(password)||password==null){
			password="11111";
		}
		dUser.set("password", MD5Util.EncoderByMd5(password));
		dUser.set("nickName", nickName);
		dUser.set("roleType", roleType);
		dUser.set("headIcon", headIcon);
		boolean isSuccess = false;
		if(userId==null||"".equals(userId)){
			isSuccess = dUser.save();
		}else{
			isSuccess = dUser.update();
		}
		System.out.println(isSuccess);
		if(isSuccess){
			renderJson("{\"error\":0,\"message\":\"\"}");
		}else{
			renderJson("{\"error\":1,\"message\":\"操作失败，请重试！\"}");
		}
	}
	
	public void deleteUser(){
		String userId = getPara("userId");
		DataUser dUser = DataUser.userDao.findById(userId);
		if(dUser.getStr("username").equals("admin")){
			renderJson("{\"error\":1,\"message\":\"该用户是系统管理员，无法删除！\"}");
		}else{
			if(DataUser.userDao.deleteById(userId)){
				renderJson("{\"error\":0,\"message\":\"\"}");
			}else{
				renderJson("{\"error\":1,\"message\":\"操作失败，请重试！\"}");
			}
		}
	}
	
	public void uploadIcon(){
		UploadFile uploadFile = getFile();
		String oldimage = getPara("oldimage"); 
		String filePath = PathKit.getWebRootPath()+"\\upload\\"+oldimage;
		if(!"".equals(oldimage)){
			File oldImageFile = new File(filePath);
			if(oldImageFile.exists()){
				System.out.println(oldImageFile.delete());
			}
		}
        renderHtml("{\"error\":0,\"data\":\""+uploadFile.getFileName()+"\"}");
	}
	
	public void chooceRole(){
		render("/admin/modal/chooceRole.jsp");
	}
}
